package com.schoolET.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.schoolET.result.MyPage;
import com.schoolET.result.Result;
import com.schoolET.service.admin.IGoodsTypeService;
import com.schoolET.service.admin.INewsService;
import com.schoolET.service.admin.IRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author: lin
 * @className: DataEchoController
 * @createTime: 2024-06-11  09:58
 * @description:
 */
@Api(tags = "数据回显")
@RestController
@RequestMapping("/schoolET")
public class DataEchoController {

    @Resource
    private IGoodsTypeService goodsTypeService;

    @Resource
    private IRegionService regionService;

    @Resource
    private INewsService newsService;


    @ApiOperationSupport(author = "lin")
    @GetMapping("/getTypeData/{id}")
    @ApiOperation("获取商品类型")
    public Result getTypeData(@PathVariable Integer id) {
        return Result.success(goodsTypeService.getGoodsTypeInfo(id)).msg("获取商品类型成功");
    }

    @ApiOperationSupport(author = "lin")
    @GetMapping("/getRegionData/{id}")
    @ApiOperation("获取区域信息")
    public Result getRegionData(@PathVariable Integer id) {
        return Result.success(regionService.getRegionInfoById(id)).msg("成功获取区域信息");
    }

    @ApiOperationSupport(author = "lin")
    @GetMapping("/getNewsData/{id}")
    @ApiOperation("获取新闻信息")
    public Result getNewsData(@PathVariable Integer id) {
        return Result.success(newsService.getNewsInfoById(id)).msg("成功获取新闻信息");
    }

    @ApiOperationSupport(author = "lin")
    @GetMapping("/getRegionalNews")
    @ApiOperation("获取特定区域新闻")
    @SaCheckLogin
    public Result getRegionalNews(@RequestParam(defaultValue = "1")Integer pageNum,
                                  @RequestParam(defaultValue = "5")Integer pageSize) {
        // 检查pageNum是否为负数，如果是，则设为默认值1
        pageNum = pageNum < 0 ? 1 : pageNum;
        // 检查pageSize是否为负数，如果是，则设为默认值5
        pageSize = pageSize < 0 ? 5 : pageSize;
        String loginIdAsString = StpUtil.getLoginIdAsString();
        return Result.success(MyPage.page(newsService.getNewsByOpenId(pageNum, pageSize, loginIdAsString))).msg("成功获取该区域新闻信息");
    }


}
